package com.bawei.yuekao.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bawei.yuekao.R;
import com.bawei.yuekao.adapter.MyFragmentPagerAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MusicFragment extends Fragment {
    private View view;
    private TabLayout tab;
    private ViewPager viewPager;
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
    private List<Fragment> mList = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_music, container, false);
        initView();
        initData();
        return view;
    }

    private void initData() {
        //避免数据重复加载
        titles.clear();
        mList.clear();
        titles.add("网络音乐");
        titles.add("本地音乐");
        mList.add(new NetMusicFragment());
        mList.add(new LocalMusicFragment());
        //注意 使用getChildFragmentManager
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,mList,titles);
        viewPager.setAdapter(mMyFragmentPagerAdapter);
        tab.setupWithViewPager(viewPager);
    }

    private void initView() {
        tab = (TabLayout) view.findViewById(R.id.tab);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager);
    }
}